package com.zf.lease.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.zf.apartment.domain.FlatsLocation;
import com.zf.apartment.domain.Location;
import com.zf.apartment.mapper.RoomMapper;
import com.zf.lease.domain.CustomerContract;
import com.zf.lease.domain.RoomContract;
import com.zf.lease.mapper.CustomerContractMapper;
import com.zf.lease.mapper.RoomContractMapper;
import com.zf.system.mapper.CustomerMapper;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zf.lease.mapper.ContractMapper;
import com.zf.lease.domain.Contract;
import com.zf.lease.service.IContractService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 契约Service业务层处理
 *
 * @author rxf
 * @date 2024-09-28
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService {
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private CustomerContractMapper customerContractMapper;
    @Autowired
    private RoomContractMapper roomContractMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private RoomMapper roomMapper;

    /**
     * 查询契约
     *
     * @param contractId 契约主键
     * @return 契约
     */
    @Override
    public Contract selectContractByContractId(Long contractId) {
        return contractMapper.selectContractByContractId(contractId);
    }

    /**
     * 查询契约列表
     *
     * @param contract 契约
     * @return 契约
     */
    @Override
    public List<Contract> selectContractList(Contract contract) {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增契约
     *
     * @param contract 契约
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract) {
        int rowsAffected = contractMapper.insertContract(contract);
        if (rowsAffected > 0) {
            CustomerContract customerContract = new CustomerContract();
            customerContract.setCustomerId(customerMapper.selectCustomerIdByCustomerName(contract.getCustomerName()));
            customerContract.setContractId(contract.getContractId());
            customerContractMapper.insertCustomerContract(customerContract);
            RoomContract roomContract = new RoomContract();
            roomContract.setRoomId(roomMapper.selectRoomByRoomName(contract.getRoomName()));
            roomContract.setContractId(contract.getContractId());
            roomContractMapper.insertRoomContract(roomContract);
        } else {
            throw new RuntimeException("Failed to insert contract");
        }
        return rowsAffected;
//            return contractMapper.insertContract(contract);
    }

    /**
     * 修改契约
     *
     * @param contract 契约
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract) {
        int rowsAffected = contractMapper.updateContract(contract);
        if (rowsAffected > 0) {
            CustomerContract customerContract = new CustomerContract();
            customerContract.setCustomerId(customerMapper.selectCustomerIdByCustomerName(contract.getCustomerName()));
            customerContract.setContractId(contract.getContractId());
            customerContractMapper.updateCustomerContract(customerContract);
            RoomContract roomContract = new RoomContract();
            roomContract.setRoomId(roomMapper.selectRoomByRoomName(contract.getRoomName()));
            roomContract.setContractId(contract.getContractId());
            roomContractMapper.updateRoomContract(roomContract);
        } else {
            throw new RuntimeException("Failed to update contract");
        }
        return rowsAffected;
    }

    /**
     * 批量删除契约
     *
     * @param contractIds 需要删除的契约主键
     * @return 结果
     */
    @Override
    public int deleteContractByContractIds(Long[] contractIds) {
        customerContractMapper.deleteCustomerContractByContractIds(contractIds);
        roomContractMapper.deleteRoomContractByContractIds(contractIds);
        return contractMapper.deleteContractByContractIds(contractIds);
    }

    /**
     * 删除契约信息
     *
     * @param contractId 契约主键
     * @return 结果
     */
    @Override
    public int deleteContractByContractId(Long contractId) {
        customerContractMapper.deleteCustomerContractByContractId(contractId);
        roomContractMapper.deleteRoomContractByContractId(contractId);
        return contractMapper.deleteContractByContractId(contractId);
    }
}